var chrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
var acceptArea = "";
jQuery.fn.showModelDiv=function showModelDiv(settingData,contentData,valueId,tipMsg,fun){
	var bodyHeight = $("body").height();
	var thisId = $(this).attr("id");
	var tipDivHeight;
	if(undefined==valueId){
		valueId = thisId.substring(0,thisId.length-4);
	}
	var tempValue = "";
	var tempV = contentData;
	try{
	contentData = eval('('+contentData+')');
	settingData = eval('('+settingData+')');
	if(valueId != undefined && valueId != "")
		$(this).val(contentData[$("#"+valueId).val()]);
	}catch(e){
	}
	function bulidStr(){
		var str = '<table width="96px" height="155px" border=0 style="background:url('+basePath+'resource/js/15.jpg) no-repeat;">';
		var count = 0;
		for(var key in contentData){
			if(count%1==0)
				if(count>0)
					str += "</tr><tr>";
				else
					str += "<tr>";
			str += "<td height='27px' style='text-align:left;cursor:hand'   onMouseOver=\"mouseOver_tip1(this)\" onMouseOut=\"mouseOut_tip1(this)\"     onclick='modelDivClick(\""+key+"\",\""+valueId+"\",\""+div_id+"\",\""+thisId+"\",\""+contentData[key]+"\",\""+fun+"\")'><span class='div_tip' onclick='modelDivClick(\""+key+"\",\""+valueId+"\",\""+div_id+"\",\""+thisId+"\",\""+contentData[key]+"\",\""+fun+"\")' ></span></td>";
			count++;
		}
		str += "</tr>";
		if(tipMsg!=undefined&&""!=tipMsg){
			str += "<tr><td colspan='3' align='left' style='color:red;text-align:left'>注意："+tipMsg+"</td></tr>";
		}
		str += "</table>";
		return str;
	}
	function bulidCss(){
		var cssStr = '';
		for(var key in settingData){
			cssStr += ";"+key+":"+settingData[key];
		}
		
		return cssStr;
	}
	var temp_val = "";
	var div_id = $(this).attr("id")+'_tip';
	$(this).focus(function(){
		try{select();}catch(e){}
		var position = $(this).position();
		temp_val = $(this).val();
		if($("#"+div_id).html()==null){
			if(undefined!=tempV&&tempV!="{}")
				var divObj = $('<div onmouseout="modelDivBlur(\''+thisId+'\');" onmouseover="modelDivFocus(\''+thisId+'\');" id="'+$(this).attr("id")+'_tip" style="'+bulidCss()+';position:absolute;left:'+position.left+'px;top:'+(position.top+21)+'px;"><div id="'+div_id+'_content" style="width:100%;background:white;border-color:white #CAD9EA #CAD9EA #CAD9EA;border-style:solid;border-width:0px">'+bulidStr()+'</div></div>').appendTo("body");		
			else
				var divObj = $('<div id="'+$(this).attr("id")+'_tip" style="'+bulidCss()+';position:absolute;left:'+position.left+'px;"><div style="width:100%;background:white;border-color:white #CAD9EA #CAD9EA #CAD9EA;border-style:solid;border-width:2px">'+bulidStr()+'</div></div>').appendTo("body");
			tipDivHeight = $("#"+div_id+'_content').height()+21;
			var elementHeight = $(this).outerHeight(true);
			var topP	= position.top+21;
			if((tipDivHeight+elementHeight+topP)>bodyHeight){
				topP = position.top - tipDivHeight-10;
			}
			$("#"+div_id).css({height:$("#"+div_id+'_content').height()+21+'px',top:topP+'px'})
		}else{
			var elementHeight = $(this).outerHeight(true);
			var topP = position.top+21;
			if((tipDivHeight+elementHeight+topP)>bodyHeight){
				topP = position.top - tipDivHeight-10;
			}
			$("#"+div_id).css({left:position.left+'px',top:topP+'px'}).show();
		}
	})
	$(this).live("blur",function(){
		if(tempValue.length>0){
		if(tempValue==$(this).val()){
			$(this).val(contentData[tempValue]);
			$("#"+valueId).val(tempValue);
		}
		}
	if(!modelDivFlag)
		$("#"+div_id).hide();
	})
	$(this).keyup(function(){
		if(undefined==tempV||tempV=="{}")
			return;
		tempValue = $(this).val();
		if(tempValue.length>0){
		$("#"+div_id).hide();
		if(tempValue!=""&&temp_val!=$(this).val()){
			if(contentData[$(this).val()]==undefined){
				alert("输入格式有误，请按照提示输入");
				$(this).focus();
				return;
			}else{
				if(valueId != undefined && valueId != ""){
					$("#"+valueId).val(tempValue);
					$(this).val(contentData[tempValue]);
					if(chrome){
						if(null!=fun&&fun!=""&&fun!=undefined){
									eval(fun);
						}
					}
				}
			}
		}
		}
		if(typeof formEditFlag != 'undefined'){
			formEditFlag = true;
		}
	})
	
	if ($.browser.msie) {
	$("#"+valueId).bind("propertychange",function(){
			try{
				//$("#"+thisId).val(contentData[$(this).val()]);
				if(null!=fun&&fun!=""&&fun!=undefined){
					eval(fun);
				}
			}catch(e){}
	})
	}else if(chrome){
	 		
	}else {        
		 document.getElementById(valueId).addEventListener("DOMAttrModified", function(){
			try{
				//$("#"+thisId).val(contentData[$(this).val()]);
				if(null!=fun&&fun!=""&&fun!=undefined){
					eval(fun);
				}
			}catch(e){}
	}, false);   
	} 
	
	
	$(this).keydown(function(){
		if(event.keyCode != 13)
			$(this).val("")
	})
	
}
var modelDivFlag = false;
var clickDivFlag = false;
function modelDivFocus(id){
	$("#"+id).die("blur");
	modelDivFlag = true;
	clickDivFlag = true;
}
function modelDivBlur(id){
	modelDivFlag = false;
	var div_id = id+'_tip';
	if(clickDivFlag)
		$("#"+id).focus();
	$("#"+id).live("blur",function(){
		if(!modelDivFlag)
		$("#"+div_id).hide();
	});
}

function modelDivClick(value,id,divid,showId,showValue,fun){
	if(typeof formEditFlag != 'undefined'){
		formEditFlag = true;
	}
	clickDivFlag = false;
	$("#"+id).die("blur");
	$("#"+id).val(value);
	if(chrome){
		//$("#"+showId).val(showValue);
		if(null!=fun&&fun!=""&&fun!=undefined){
					eval(fun);
		}
	}
	$("#"+divid).hide();
	try{
		aaaa();
	}catch(e){}
}

function mouseOver_tip(obj){
	$(obj).css("background-color","#4DB5E4")
}

function mouseOut_tip(obj){
	$(obj).css("background-color","white")
}

function mouseOver_tip1(obj){
	$(obj).css("background-image","url("+basePath+"resource/js/15.png)")
}

function mouseOut_tip1(obj){
	$(obj).css("background","")
}

function handle(value,ShowId){
	$("#ShowId").val(value);
}

